<template>
  <div class="attachment-management">
    <div class="head">
      <el-form size="small" :inline="true">
        <el-form-item label="搜索">
          <el-input @input="inputChange" v-model="searchInput" placeholder="输入附件编号或名称进行检索" class="search_input"/>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" plain icon="el-icon-plus" size="small" @click="openAddDialog">新增附件</el-button>
        </el-form-item>
        <el-form-item>
          <el-button :disabled="selectedAttachments.length === 0" type="danger" plain icon="el-icon-delete" size="small" @click="deleteAttachments">删除附件</el-button>
        </el-form-item>
      </el-form>
      <!--分页组件 v-show:是否显示，total：总数，page.sync：第几页，limit.sync：一页几个，pagination：触碰事件-->
      <pagination class="pagination"
                  v-show="total>0"
                  :total="total"
                  :page.sync="queryParams.pageNum"
                  :limit.sync="queryParams.pageSize"
                  @pagination="fetchAttachments"/>
    </div>

    <div class="body">
      <el-table :data="attachments" style="width: 100%" @selection-change="handleSelectionChange">
        <el-table-column type="selection" width="55"></el-table-column>
        <el-table-column prop="attachmentId" label="附件编号" sortable></el-table-column>
        <el-table-column prop="attachmentName" label="附件名称"></el-table-column>
        <el-table-column label="操作">
          <template v-slot:default="scope">
            <el-button size="mini" type="primary" @click="openEditDialog(scope.row)">编辑</el-button>
            <el-button size="mini" type="danger" plain @click="deleteAttachment(scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>

    <el-dialog :title="flag === 0 ? '新增附件' : '编辑附件'" :visible.sync="dialogVisible">
      <el-form :model="currentAttachment">
        <el-form-item label="附件名称">
          <el-input v-model="currentAttachment.attachmentName" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="附件备注">
          <el-input v-model="currentAttachment.description" autocomplete="off" type="textarea" :autosize="{ minRows: 2, maxRows: 4}"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取消</el-button>
        <el-button v-show="flag==0" type="primary" @click="addAttachment">新增</el-button>
        <el-button v-show="flag==1" type="primary" @click="updateAttachment">保存</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import {
  addAnnex,
  getAnnexList,
  deleteAnnex,
  updateAnnex,
  getAnnex
} from '@/api/system/annex';

export default {
  data() {
    return {
      searchInput: '',
      attachments: [],
      selectedAttachments: [],
      dialogVisible: false,
      currentAttachment: { attachmentId: '', attachmentName: '', description: '' },
      flag: 0,
      total:0,//题目总数
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
      },
    };
  },
  mounted() {
    this.fetchAttachments();
  },
  methods: {
    handleSelectionChange(selection) {
      this.selectedAttachments = selection.map(item => item.attachmentId);
    },
    inputChange() {
      if (this.searchInput.trim() !== "") {
        getAnnex(this.searchInput).then(res => {
          if (res.code == 200) {
            this.attachments = [res.data];
          }
        });
      } else {
        this.fetchAttachments();
      }
    },
    fetchAttachments() {
      this.attachments=[];
      getAnnexList(this.addDateRange(this.queryParams, this.dateRange)).then(res => {
        console.log("接受附件列表",res);
        this.total=res.total;
        res.rows.forEach(item=>{
          this.attachments.push({
            attachmentId:item.attachmentId,
            attachmentName:item.attachmentName,
            description:item.description
          })
        })
      }).catch(error => {
        console.error("Error fetching attachments:", error);
      });
    },
    openAddDialog() {
      this.flag = 0;
      this.currentAttachment = { attachmentId: '', attachmentName: '', description: '' };
      this.dialogVisible = true;
    },
    openEditDialog(attachment) {
      this.flag = 1;
      this.currentAttachment = { ...attachment };
      this.dialogVisible = true;
    },
    addAttachment() {
      addAnnex(this.currentAttachment).then(() => {
        this.fetchAttachments();
        this.dialogVisible = false;
      });
    },
    updateAttachment() {
      updateAnnex(this.currentAttachment).then(() => {
        this.dialogVisible = false;
        this.fetchAttachments();
      });
    },
    deleteAttachments() {
      this.selectedAttachments.forEach(attachmentId => {
        deleteAnnex(attachmentId).then(() => {
          this.fetchAttachments();
        });
      });
      this.selectedAttachments = [];
    },
    deleteAttachment(attachment) {
      deleteAnnex(attachment.attachmentId).then(() => {
        this.fetchAttachments();
      });
    }
  }
};
</script>

<style scoped>
.attachment-management {
  padding: 20px;
}
.search_input {
  width: 200px;
}
.head{
  position: relative;
}
.pagination{
  position: absolute;
  top:-50%;
  right: 0;
}
</style>
